<template>
  <div>
    <template class="c-field" v-if="display">{{ display }}</template>
    <template v-else>
      <span class="c-field-placeholder">{{ placeholder }}</span>
    </template>
    <van-popup
      v-model="visible"
      position="bottom"
      get-container="body"
      class="c-hfni2f"
    >
      <div class="c-hfni2f-title">{{ title }}</div>
      <div
        class="c-hfni2f-item"
        v-for="item in columns"
        :key="item.index"
        :class="item.value == value ? 'c-hfni2f-item-active' : ''"
        @click="handleClick(item)"
      >
        {{ item.label }}
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    defaultValue: {
      type: String,
      default: ''
    },
    columns: {
      type: Array,
      default: () => []
    },
    inputType: {
      type: String,
      default: '' //bank银行 ，card证件号， relation是投保人的
    }
  },
  computed: {
    display() {
      const item = this.columns.find(item => item.value === this.value)
      return item ? item.label : ''
    }
  },
  data() {
    return {
      visible: false,
      value: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.value = this.defaultValue
    },
    handleClick(item) {
      this.$emit('confirm', item.value)
      this.value = item.value
      this.visible = false
    }
  }
}
</script>
<style lang="less" scoped>
.c-field {
  font-size: 15px;
  color: #333;
}
.c-field-placeholder {
  color: #999;
  font-size: 15px;
}
.c-hfni2f {
  max-height: 500px;
  border-radius: 13px 13px 0 0;
  text-align: center;
  padding: 15px;
  box-sizing: border-box;
  &-title {
    font-size: 18px;
    color: #333;
    font-weight: bold;
  }
  &-item {
    font-size: 15px;
    color: #666;
    margin-top: 15px;
    padding-bottom: 10px;
    border-bottom: 0.5px solid #e9e9e9;
    &:last-child {
      border-bottom: none;
    }
  }
  &-item-active {
    color: @c-color-primary;
  }
}
</style>
